package com.graduate.twentyone.controller;

import com.graduate.twentyone.common.R;
import com.graduate.twentyone.domain.SlideShow;
import com.graduate.twentyone.service.SlideShowService;
import com.graduate.twentyone.util.UploadConfig;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@Api(value = "SlideShow控制类", tags = "SlideShow管理相关")
@CrossOrigin
@RequestMapping("/slideshow")
public class SlideShowController {

    @Autowired
    private SlideShowService slideShowService;

    @Autowired
    private UploadConfig uc;


    @GetMapping("/all")
    public R queryAll() {
        return R.ok().data("slideshowlist", slideShowService.all());
    }

    @RequiresPermissions("admin:all")
    @PatchMapping("/update")
    public R updateById(SlideShow ss,MultipartFile mf) {
        System.out.println("mf = " + mf);
        SlideShow slideShow = new SlideShow(ss.getSlideshow_id(),null,ss.getInfor());
        if(mf != null){
            String s = uc.upLoadFoodsImag(mf);
            slideShow.setPicture(s);
        }
        return R.ok().data("Advertisement",slideShowService.updateById(slideShow));
    }

    @RequiresPermissions("admin:all")
    @PostMapping("/delete")
    public R deleteById(@RequestBody Map map){
        System.out.println("(Integer)map.get " + (Integer)map.get("slideshow_id"));
        return R.ok().data("Advertisement",slideShowService.deleteById((Integer)map.get("slideshow_id")-0));
    }

    @RequiresPermissions("admin:all")
    @PostMapping("add")
    public R addOne(SlideShow ss,MultipartFile mf){
        SlideShow slideShow = new SlideShow(1,uc.upLoadFoodsImag(mf),ss.getInfor());
        return R.ok().data("Advertisement",slideShowService.addOne(slideShow));
    }
}
